﻿using Common.Contract.Customer.Record;
using Common.Contract.Customer.Request;
using Common.Contract.Customer.Response;
using Common.Service.Command;
using Customer.Model.Interface;
using Customer.Model.Model;
using System;
using System.Collections.Generic;

namespace Customer.Core.ServiceCommand
{
    public class RetrieveRelCusInfoByCifServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveRelCusInfoRequest, RetrieveRelCusInfoResponse>
    {
        #region Properties
        public string Cif;
        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        #endregion
        public RetrieveRelCusInfoByCifServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveRelCusInfoRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (string.IsNullOrEmpty(request.RelCif))
            {
                throw new ArgumentException("Cif Customer must be set", "request");
            }
            else
            {
                Cif = Request.RelCif;
            }
            if (Request.Page != null)
            {
                pageIndex = Request.Page.PageIndex ?? 0;
                pageSize = Request.Page.PageSize ?? 0;
            }
        }

        protected override bool OnExecute(RetrieveRelCusInfoResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            List<RelCusInfoRecord> records = new List<RelCusInfoRecord>();
            CustomerRelation customerRelation = new CustomerRelation();
            customerRelation = Providers.CustomerAlerts.RetrieveCustomerRelationByCif(Cif);
            RelCusInfoRecord relCustInfo = new RelCusInfoRecord
            {
                RelCusName = customerRelation.CusRelName,
                RelCusAddress = customerRelation.AddressRelCus,
                RelCusEmail = customerRelation.EmailRelCus,
                RelCusMobile = customerRelation.MobileRelation,
                RelCusGender = customerRelation.Gender,
                RelCusPosition = customerRelation.CusRelPos
            };
            records.Add(relCustInfo);
            response.Records = records;

            return isSuccess;
        }
    }
}
